""" 
wifi连接
"""

from machine import Pin
import time
import network

led1 = Pin(15, Pin.OUT)
ssid = "esp32_test_wifi"
password = "12356789"


def wifi_connec():
    wlan = network.WLAN(network.STA_IF)  # STA模式
    wlan.active(True)   # 激活,连接wifi
    start_time = time.time()

    if not wlan.isconnected():
        print("connecting to network...")
        wlan.connect(ssid, password)
        while not wlan.isconnected():
            if time.time() - start_time > 15:
                print("connect timeout")
                return

            led1.value(1)
            time.sleep(300)
            led1.value(0)
            time.sleep(300)
    else:
        led1.value(1)
        print("wifi connected:")
    print("network config:", wlan.ifconfig())


if __name__ == "__main__":
    wifi_connec()
